﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class UserWork_SendPost : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            //con1.Open();
            //string sql1 = "select AppurtenanceName from Appurtenance";
            //SqlCommand com1 = new SqlCommand(sql1, con1);
            //SqlDataReader dr1 = com1.ExecuteReader();
            //while (dr1.Read())
            //{
            //    string url = Server.MapPath("~") + "\\upfiles\\" + dr1["AppurtenanceName"].ToString();
            //    if (File.Exists(url))
            //    {
            //        File.Delete(url);
            //    }
            //}
           // con1.Close();
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "delete from Appurtenance where 1=1";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            con.Close();
          
        }
        Bind();
    }
    protected void bnSel1_ServerClick(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.open('MailSend.aspx','','width=390,height=370,top=250,left=280')</script>");
        Response.Write("<script>location='javascript:history.go(-1)'</script>");
    }
   public static string src = "";
    protected void bnUpload_ServerClick(object sender, EventArgs e)
    {
        try
        {

            if (FileUpload1.PostedFile.ContentLength == 0)
            {
                src = "";
            }
            else
            {

                string strFilePath = FileUpload1.PostedFile.FileName;
                FileInfo fl = new FileInfo(strFilePath);
                string Ext = fl.Name;
                //string i = fl.Name;

                src = Ext;
                string ServerPath = Server.MapPath("~");
                string strSeraPath = ServerPath + "\\upfiles\\" + src;
                FileUpload1.PostedFile.SaveAs(strSeraPath);
                this.MyPanel.Visible = true;
                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con.Open();
                string sql = "insert Appurtenance values('" + Session["UserName"] + "','" + src + "')";
                SqlCommand com = new SqlCommand(sql, con);
                com.ExecuteNonQuery();
                con.Close();

                this.MyPanel.Visible = true;
                Bind();
                this.Label1.Text = "上传成功";
            }
        }
        catch 
        {
            this.Label1.Text = "只能上传5M以下的文件.";
        }
    }
    public void Bind()
    {
        SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con1.Open();
        string sql1 = "select * from Appurtenance ";
        SqlDataAdapter da = new SqlDataAdapter(sql1, con1);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.GridView1.DataSource = ds;
        this.GridView1.DataBind();
        con1.Close();
    }


    protected void  GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int Appurtenanceid=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string url=Server.MapPath("~")+"\\upfiles\\"+src;
        if(File.Exists(url))
        {
            File.Delete(url);
        }

        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "delete from Appurtenance where Appurtenanceid="+Appurtenanceid+"";
        SqlCommand com = new SqlCommand(sql,con);
        com.ExecuteNonQuery();
        this.Label1.Text = "删除成功";
        con.Close();
        Bind();

    }
    protected void bnSend_ServerClick(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string Title = this.txTitle.Value.Trim();
        string Type = DropDownList1.SelectedItem.Text;
        string Content = this.txContent.Value.Trim();
        string SendName = Session["UserName"].ToString();
        string MeetName = this.txtOtherMan.Value.Trim();
        DateTime Time = DateTime.Now;
        string Status = "未读";
        ArrayList al = new ArrayList();
        string App="";
        for (int i = 0; i <GridView1.Rows.Count; i++)
        {
         
            al.Add(GridView1.Rows[i].Cells[0].Text.ToString());
        }
        for (int j = 0; j < al.Count; j++)
        {
             App+=al[j].ToString()+";";
        }
        string[] a;
        a = MeetName.Split(';');
        for (int i = 0; i < a.Length-1; i++)
        {
            string sql = "insert into Email values('" + Title + "','" + Type + "','" + Content + "','" + SendName + "','" + a[i] + "','" + Time + "','" + Status + "','" + App + "')";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            
        }

        SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con1.Open();
        string sql1 = "insert into SendMail values('"+Title+"','"+Type+"','"+Content+"','"+SendName+"','"+MeetName+"','"+Time+"','"+App+"')";
        SqlCommand com1 = new SqlCommand(sql1,con1);
        com1.ExecuteNonQuery();
        con1.Close();
        Response.Write("<script>alert('发送成功！');window.location.href='MailListF.aspx';</script>");
        
 

    }
    protected void bnSave_ServerClick(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string Title = this.txTitle.Value.Trim();
        string Type = DropDownList1.SelectedItem.Text;
        string Content = this.txContent.Value.Trim();
        string SendName = Session["UserName"].ToString();
        string MeetName = this.txtOtherMan.Value.Trim();
        ArrayList al = new ArrayList();
        string App = "";
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            al.Add(GridView1.Rows[i].Cells[0].Text.ToString());
        }
        for (int j = 0; j < al.Count; j++)
        {
            App += al[j].ToString() + ";";
        }
        DateTime dt = DateTime.Now;
        string[] a;
        a = MeetName.Split(';');
        string sql = "insert into Draft values('"+dt+"','" + Title + "','" + Type + "','" + Content + "','" + MeetName + "','" + App + "','" + Session["UserName"].ToString() + "')";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            Response.Write("<script>alert('保存成功！');window.location.href='MailListC.aspx';</script>");

        
    }
}
